//引入依赖
import Router from 'koa-router';
import axios from './utils/axios';

let router = new Router({
  //让文件路由都有前缀
  prefix: '/geo'
});

//http://cp-tools.cn/geo的API签名信息
const sign = 'b735ab0b3e46d12ec07336ec5195a95f';

//接口编写
//请求地理位置
router.get('/getPosition', async (ctx) => {
  let {
    status,
    data: {
      province,
      city
    }
  } = await axios.get(`http://cp-tools.cn/geo/getPosition?sign=${sign}`);

  if (status === 200) {
    ctx.body = {
      province,
      city
    }
  } else {
    ctx.body = {
      province: '',
      city: ''
    }
  }
});

//请求菜单信息
router.get('/menu', async (ctx) => {
  let {
    status,
    data: {
      menu
    }
  } = await axios.get(`http://cp-tools.cn/geo/menu?sign=${sign}`);

  if (status === 200) {
    ctx.body = {
      menu
    }
  } else {
    ctx.body = {
      menu: []
    }
  }
});

//请求省份
router.get('/province', async (ctx) => {
  let {
    status,
    data: {
      province
    }
  } = await axios.get(`http://cp-tools.cn/geo/province?sign=${sign}`);

  if (status === 200) {
    ctx.body = {
      province
    }
  } else {
    ctx.body = {
      menu: []
    }
  }
})

//省份动态
router.get('/province/:id', async (ctx) => {
  let {
    status,
    data: {
      city
    }
  } = await axios.get(`http://cp-tools.cn/geo/province/${ctx.params.id}?sign=${sign}`);

  if (status === 200) {
    ctx.body = {
      city
    }
  } else {
    ctx.body = {
      city: []
    }
  }
})

//请求城市
router.get('/city', async (ctx) => {
  let {
    status,
    data: {
      city
    }
  } = await axios.get(`http://cp-tools.cn/geo/city?sign=${sign}`);

  if (status === 200) {
    ctx.body = {
      city
    }
  } else {
    ctx.body = {
      city: []
    }
  }
})

//热门城市
router.get('/hotCity', async (ctx) => {
  let {
    status,
    data: {
      hots
    }
  } = await axios.get(`http://cp-tools.cn/geo/hotCity?sign=${sign}`);

  if (status === 200) {
    ctx.body = {
      hots
    }
  } else {
    ctx.body = {
      hots: []
    }
  }
})

export default router;
